package com.baymax.test.scene.demo

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.*
import android.view.ViewGroupOverlay
import android.view.ViewParent
import android.widget.FrameLayout
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import com.baymax.test.scene.*
import com.baymax.test.scene.drawable.DrawableUtils
import com.baymax.test.scene.drawable.InvlateDrawable
import com.baymax.test.scene.helper.RoundingHelper.Drawer
import com.baymax.test.scene.view.BlurDrawable
import com.baymax.test.scene.view.BreathImage
import com.bytedance.scene.group.GroupScene
import com.bytedance.scene.ktx.replace
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.drawable.ArrayDrawable
import com.facebook.drawee.view.SimpleDraweeView
import kotlin.properties.Delegates

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       LayerTypeTest
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2023/4/27 14:43
 * @Copyright (C) 2023 YSTEN
 * @author:       xuyujian
 */
class LayerTypeTest : GroupScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): ViewGroup {
        return p1.newSceneFlexContainer {

//            addView(SurfaceView(context) ,  MarginLayoutParams(1 , 1))

            val flex = this
            fun newBreath() = BreathImage(context).apply {
                background = InvlateDrawable()
            }

            val testLayer = newBreath()

            val fr = FrameLayout(context).apply {
                addView(testLayer, MarginLayoutParams(200, 200).apply {
                    leftMargin = 60
                    topMargin = 60
                })
            }

            background = ColorDrawable(0x1bffffff)


            val blur = BlurDrawable()

            val draws = blur

            val target = FrameLayout(context).apply {
                setBackgroundResource(R.mipmap.ic_launcher_round)
            }
            addButton("set padd") {

                (target.parent as ViewGroup).apply {
                    val p = this.paddingLeft
                    val e = if (p != 0) 0 else 30
                    setPadding(e, e, e, e)
                }
            }

            var corner by Delegates.observable(draws.corner){ p , o ,n ->
                draws.corner = n
            }
            addButton("setCorner"){
                corner += 5
                if (corner >= draws.bounds.height() / 2) {
                    corner = 0f
                }
            }

            addButton("layer flex") {
                flex.toggleLayer()
            }

            addButton("layer") {
                (target.parent as ViewGroup).toggleLayer()
            }



            addButton("start ") {

                val overlay = target.overlay
                if (target is ViewGroup) {
                    target.setWillNotDraw(false)
                }
                blur.breathStyle = (Math.random() * 10 % 2).toInt()
                val array = ArrayDrawable(
                    arrayOf(
                        blur,
//                        DrawableUtils.createGradientDrawable(Color.GREEN, 2, 8f).apply {
//                            setPadding( 4,4,4,4)
//                        }
                    )
                )
                overlay.add(array)
//                draws.offset = 0f
                array.setBounds(0, 0, target.width, target.height)
                draws.start()

            }

            addButton("stop") {
                val overlay = target.overlay
                draws.stop()
                overlay.clear()

            }

            addButton("change border") {
                draws.corner = 0f
            }
            addButton("setBG") {
                val bg = target.background
                if (bg != null) {
                    target.background = null
                } else {
                    target.background =
                        DrawableUtils.createGradientDrawable(0xffff00ff.toInt(), 10f)
                }
            }

            addView(FrameLayout(context).apply {
                post {
                    var parent: ViewGroup? = this
                    while (parent != null) {
                        parent.clipChildren = false
                        parent.clipToPadding = false
                        parent = parent.parent as? ViewGroup
                    }
                }
                addView(target, -1, -1)
                val overlay = target.overlay
//                val overlay = this.overlay
//                background = ColorDrawable(Color.BLACK)
                isFocusable = true
                val v1 = blur
                setOnFocusChangeListener { v, hasFocus ->

                    val to = if (hasFocus) 1.1f else 1.0f
                    v.animate().scaleX(to).scaleY(to).start()
                    draws.setBounds(
                        -draws.corner.toInt(),
                        -draws.corner.toInt(),
                        target.width + draws.corner.toInt(),
                        target.height + draws.corner.toInt()
                    )

                    if (hasFocus) {
                        overlay.add(v1)
                        v1.start()
                    } else {
                        overlay.remove(v1)
                        v1.stop()
                    }
                }

            }, MarginLayoutParams(240, 240))



            addButton("test") {
                this.toggleLayer()
                val b = BlurDrawable()
                overlay.add(b)
                b.setBounds(0, 0, width, height)
                b.start()
            }
            newFlexContainer {

                addButton("test") {

                    val b = BlurDrawable()
                    background = b
                    b.start()
                }
            }


            addView(SimpleDraweeView(context).apply {
                setImageURI("res://" + context.packageName + "/" + R.mipmap.audio_play_black)

            }, MarginLayoutParams(80, 80))

            val scrollView = object : HorizontalScrollView(context) {
                init {

                    clipChildren = false
                    clipToPadding = false
                }

                override fun dispatchDraw(canvas: Canvas) {
                    super.dispatchDraw(canvas)
                    canvas.clipRect(
                        scrollX + paddingLeft, 0,
                        scrollX + width - paddingRight,
                        height,
                    );
                }
            }
            val child = LinearLayout(context).apply {
                orientation = LinearLayout.HORIZONTAL
            }
            scrollView.addView(child, ViewGroup.LayoutParams(-1, 100))
            repeat(100) {
                child.addView(TextView(context).apply {
                    setTextSize(TypedValue.COMPLEX_UNIT_PX, 48f)
                    setText(it.toString())
                }, LinearLayout.LayoutParams(300, 100))
            }

            addView(scrollView)

            val frame = FrameLayout(context).apply {
                id = View.generateViewId()
            }
            addView(frame, MarginLayoutParams(400, 400).apply {
                marginStart = 60
            })

            replace(frame.id, SurfaceScene(), "surface")
        }


    }


    private fun dumpInfo(v: View) {
        var current: View? = v
        while (current != null) {
            Log.w("Layer", String.format("dumpInfo current:%s ,%s", current.layerType, current))
            current = current.parent as? View
        }
    }

    fun View.toggleLayer() {
        if (this.layerType == View.LAYER_TYPE_SOFTWARE) {
            this.setLayerType(View.LAYER_TYPE_NONE, null)
        } else {
            this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        }
        dumpInfo(this)

    }


}